<?php
/**
 * Created by PhpStorm.
 * User: 吴登科
 * Date: 2018/10/31
 * Time: 14:42
 * 订单详情
 */

namespace Home\Controller;

use Think\Controller;
use think\Session;

class OrderController extends Controller
{
    public $customer_id = '';
    public $store_id = 0;

    /*
     * 初始化
     */
    public function _initialize()
    {
        if (session('?customer_id') && session('?STORE_ID')) {
            $this->customer_id = session('customer_id');//用户id
            $this->store_id = session('STORE_ID');//店铺id
        }
        $this->customer_id = "20181102115031877722";
        $this->store_id = '11';
    }

    /**
     * 我的订单
     */
    public function index(){
        $this->display();
    }

    /**
     * 获取订单信息
     */
    public function getOrderList(){
        //获得某个用户在某个店铺下的订单 1 没有评论过 2 评论过
        if ($this->store_id && $this->customer_id) {
            //获取未付款 未评论

            if(I('get.is_comment')){
                $where['is_comment'] = I('get.is_comment');
            }

            $where['valid_flag'] = 1;//有效订单
            $where['customer_id'] = $this->customer_id;
            $where['store_id'] = $this->store_id;
            $dish_order = M('dishes_order')->field("id,customer_id,order_no,store_id,FROM_UNIXTIME(time,'%Y-%m-%d %H:%i:%s')as createTime,desk_number,people_number,remark,order_state,total_price,pay_time,valid_flag,order_price,discount_price,invoice_status,is_comment")->where($where)->order("time desc")->select();
            //根据订单id查询订单详情
            if($dish_order){
                $order_array = array();
                foreach ($dish_order as $key => $v) {
                    switch ($v['order_state']) {
                        case 1:
                            $dish_order[$key]['fromart_state'] = '待付款';
                            break;
                        case 2:
                            $dish_order[$key]['fromart_state'] = '已付款';
                            break;
                    }
                    switch ($v['is_comment']) {
                        case 1:
                            $dish_order[$key]['fromart_is_comment'] = '未评论';
                            break;
                        case 2:
                            $dish_order[$key]['fromart_is_comment'] = '已评论';
                            break;
                    }
                    $order_array[] = $v['order_no'];
                }
                $order_array = implode(',', $order_array);
                $order['order_no'] = array('in', $order_array);

                //获取相同订单里面的order_no
                $dish_detail =  M('order_detail')->alias('o')->join("join meal_dishes as d on d.id = o.dish_id")->field('o.order_no,o.dish_id,o.count,d.name,count(o.order_no) as allCont,d.image')->where($order)->order("o.create_time desc")->select();

                foreach ($dish_order as &$v){
                    foreach ($dish_detail as $val){
                        if($v['order_no'] == $val['order_no']){
                            $v['dish_id'] = $val['dish_id'];
                            $v['dish_name'] = $val['name'];
                            $v['allcont'] = $val['allcont'];
                            $v['image'] = $val['image'];
                        }else{
                            $v['dish_id'] = "";
                            $v['dish_name'] = "";
                            $v['allcont'] = "";
                            $v['image'] = "";
                        }
                    }
                }
                $this->ajaxreturn(array('state' => 1, 'data' => $dish_order,'msg'=>'获取订单成功！'), 'json');
            }else{
                $this->ajaxreturn(array('state' => 2, 'data' => [],'msg'=>'没有订单！'), 'json');
            }
        } else {
            $this->error("服务器繁忙请稍后再试！", U('Home/Index/index'));
        }
    }

    /**
     * 点餐下单
     */
    public function order()
    {
        if ($this->store_id && $this->customer_id) {
            $data['store_id'] = $this->store_id;
            $data['customer_id'] = $this->customer_id;
            $data['order_no'] = ordersn();
            $data['time'] = time();
            //桌号ID
            $data['desk_number'] = I('post.desk_number');
            $data['people_number'] = I('post.people_number');
            $data['remark'] = I('post.remark');
            //最终支付的价钱
            $data['total_price'] = I('post.total_price');
            //订单的价格
            $data['order_price'] = I('post.order_price');
            //优惠的金额
            $data['discount_price'] = I('post.discount_price');
            //开票状态0待开票1已开票
            $data['invoice_status'] = I('post.invoice_status');

            $result = M('dishes_order')->add($data);

            //购物车的内容
            $list = I('request.cart');

            $a = file_get_contents('php://input');
            $list = json_decode($a,true);
            $list = $list['cart'];

            $countList = count($list);

            foreach ($list as $v){
                $data_detail['order_no'] = $data['order_no'];
                $data_detail['dish_id'] = $v['dish_id'];
                $data_detail['count'] = $v['count'];
                $data_detail['price'] = $v['price'];
                $data_detail['amount'] = $v['price'] * $v['count'];
                $data_detail['create_time'] = time();
                $data_detail['practice'] = $v['practice'];
                $data_detail['num'] = $v['num'];
                $result_detail = M("order_detail")->add($data_detail);
            }

            if ($result && $result_detail) {
                $this->ajaxreturn(array('state' => 1,'data'=>$result,'msg'=>'成功加入购物车') , 'json');
            } else {
                $this->ajaxreturn(array('state' => -1,'msg'=>'加入购物车失败') , 'json');
            }

        }else{
            $this->error("服务器繁忙请稍后再试！", U('Home/Index/index'));
        }
    }

}